$barcode = $ARGV[0];
chomp($barcode);
$spikedin = $ARGV[1];
chomp($spikedin);
$mad = $ARGV[2];
chomp($mad);
$present = $ARGV[3];
chomp($present);
$ref = $ARGV[4];
chomp($ref);
$outfile = $ARGV[5];
chomp($outfile);
open(BUFF1,"$barcode");
open(BUFF2,"$spikedin");
open(BUFF3,"$mad");
open(BUFF4,"$present");
open(BUFF5,"$ref");
open(WRBUFF,">$outfile");
print WRBUFF "SAMPLE_NAME\tQC_SAMPLE_FLAG\tPRESENT_ABSENT\tBARCODE\tMAD\tSPIKE\tQC_SAMPLE_IND\tQC_SAMPLE_FLAG\n";
$i=0;
$line = <BUFF5>;
while($line = <BUFF5>)
{
	chomp($line);
	@array = split(/\t/,$line);
	$barcode[$i] = $array[1];
	$spikedin[$i] = $array[2];
	$mad[$i] = $array[4];
	$present[$i] = $array[3];	
	$i++;
}
@barcode1 = sort {$b <=> $a} @barcode;
@spikedin1 = sort {$b <=> $a} @spikedin;
@mad1 = sort {$b <=> $a} @mad;
@present1 = sort {$b <=> $a} @present;

$kk = "barcode1";
while($line = <BUFF1>)
{
        chomp($line);
	#print "testtest $line\n";
	$line =~ s/\.gz//g;
	@arry = split(/\t/,$line);
	#print $arry[1]."\n";
	$i=0;
	$k =0;
	while($i<@{"$kk"} && $k ==0)
	{
		if($arry[1] <= ${"$kk"}[$i])
		{
			$number = $i+1;	
			#print "$arry[1]\t${"$kk"}[$i]\n";
		}
		$i++;	
	}		
	$totalnumber = @{"$kk"};

	$percentile = (($totalnumber-$number)/$totalnumber)*100;
	#print "$arry[0]\t$percentile\n";
	$hash{$arry[0]} = $percentile;
}

$kk = "spikedin1";
while($line = <BUFF2>)
{
        chomp($line);
        $line =~ s/\.gz//g;
        @arry = split(/\t/,$line);
        #print $arry[1]."\n";
        $i=0;
        $k =0;
        while($i<@{"$kk"} && $k ==0)
        {
                if($arry[1] <= ${"$kk"}[$i])
                {
                        $number = $i+1;
                        #print "$arry[1]\t${"$kk"}[$i]\n";
                }
                $i++;
        }
        $totalnumber = @{"$kk"};
        $percentile = (($totalnumber-$number)/$totalnumber)*100;
        #print "$arry[0]\t$percentile\n";
	if(exists($hash{$arry[0]}))
	{
        	$hash{$arry[0]} = $hash{$arry[0]}."\t".$percentile;
	}
	else
	{
		print "something wrong $arry[0] spikedin\n";
	}
}

$kk = "mad1";
while($line = <BUFF3>)
{
        chomp($line);
        $line =~ s/\.gz//g;
        @arry = split(/\t/,$line);
        #print $arry[1]."\n";
        $i=0;
        $k =0;
        while($i<@{"$kk"} && $k ==0)
        {
                if($arry[1] <= ${"$kk"}[$i])
                {
                        $number = $i+1;
                        #print "$arry[1]\t${"$kk"}[$i]\n";
                }
                $i++;
        }
        $totalnumber = @{"$kk"};
        $percentile = (($totalnumber-$number)/$totalnumber)*100;
        #print "$arry[0]\t$percentile\n";
        if(exists($hash{$arry[0]}))
        {
                $hash{$arry[0]} = $hash{$arry[0]}."\t".$percentile;
        }
        else
        {
                print "something wrong $arry[0] mad\n";
        }
}
$kk = "present1";
while($line = <BUFF4>)
{
        chomp($line);
        $line =~ s/\.gz//g;
        @arry = split(/\t/,$line);
        #print $arry[1]."\n";
        $i=0;
        $k =0;
        while($i<@{"$kk"} && $k ==0)
        {
                if($arry[1] <= ${"$kk"}[$i])
                {
                        $number = $i+1;
                        #print "$arry[1]"."\t".${"$kk"}[$i]."\t".$i."\n";
                }
                $i++;
        }
        $totalnumber = @{"$kk"};
        $percentile = (($totalnumber-$number)/$totalnumber)*100;
        #print "$arry[0]\t$percentile\n";
        if(exists($hash{$arry[0]}))
        {
                $hash{$arry[0]} = $hash{$arry[0]}."\t".$percentile;
        }
        else
        {
                print "something wrong $arry[0] present\n";
        }
}
while(($key,$value) = each %hash)
{
	#print $key."\t".$value."\n";
	@arr = split(/\t/,$value);
	unshift(@arr,$key);
	#print @arr."\n";
	$ind = 1;
	$remove = 1;
	if($arr[1] > 99.5 && $arr[1] < 0.5)
	{
		$remove = 0;
	}		
	if($arr[2] > 99.5 && $arr[2] < 0.5)
        {
                $remove = 0;
        }
	if($arr[3] > 99)
        {
                $remove = 0;
        }
	if($arr[4] > 99.5 && $arr[4] < 0.5)
        {
                $remove = 0;
        }
	if($arr[1] > 97.5 && $arr[1] < 2.5)
        {
                $ind = 0;
        }
        if($arr[2] > 97.5 && $arr[2] < 2.5)
        {
                $ind = 0;
        }
        if($arr[3] > 95)
        {
                $ind = 0;
        }
        if($arr[4] > 97.5 && $arr[4] < 2.5)
        {
                $ind = 0;
        }
	#print "$ind\t$remove\n";
	#print @arr."\n";
	if($remove == 1)
	{
		$remove11 = "Y";
	}
	else
	{
		$remove11 = "N";
	}
	print WRBUFF $arr[0]."\t".$remove11."\t".$arr[4]."\t".$arr[1]."\t".$arr[3]."\t".$arr[2]."\t".$ind."\t".$remove."\n";
		
}

